<?php
// $Header: /cvsroot/tsheet/timesheet.php/task_action.php,v 1.7 2005/05/23 07:32:00 vexil Exp $
// Authenticate
require("class.AuthenticationManager.php");
require("class.CommandMenu.php");
if (!$authenticationManager->isLoggedIn() || !$authenticationManager->hasClearance(CLEARANCE_ADMINISTRATOR)) {
	Header("Location: login.php?redirect=$_SERVER[PHP_SELF]&clearanceRequired=Administrator");
	exit;
}

// Connect to database.
$dbh         = dbConnect();
$contextUser = strtolower($_SESSION['contextUser']);
//load local vars from superglobals
$action      = $_REQUEST["action"];
$task_id     = isset($_REQUEST["task_id"]) ? $_REQUEST["task_id"]: 0;
$proj_id     = $_REQUEST["proj_id"];

if ($action == "add" || $action == "edit") {
	$name           = $_REQUEST["name"];                                              // Task Name
	$identifier     = $_REQUEST["identifier"];                                        // Task Identifier
	$description    = $_REQUEST["description"];                                       // Task Description
	$source_project = $_REQUEST["source_project"];                                    // Task source project
	$target_project = $_REQUEST["target_project"];                                    // Task target project
	$task_status    = $_REQUEST["task_status"];                                       // Task Status
	$manager        = $_REQUEST["manager"];                                           // Task Manager
	$assigned       = isset($_REQUEST["assigned"]) ? $_REQUEST['assigned']: array();  // Task Assignments
	$task_cir       = $_REQUEST["task_cir"];                                               // Task eligible for calculating the CIR
} 

if (!isset($action))
{
    Header("Location: $HTTP_REFERER");
}
elseif ($action == "add") 
{
	$name        = addslashes($name);
	$description = addslashes($description);

	//create a time string for >>now<<
	$time_string = date("Y-m-d H:i:00");		
		
	list($qh, $num) = dbQuery("INSERT INTO $TASK_TABLE (proj_id, name, identifier, description, source_project, target_project, manager, assigned, started, status, cir) VALUES ".
			                  "('$proj_id', '$name', '$identifier', '$description', '$source_project', '$target_project', '$manager', ".
			                  "'$time_string', '$time_string', '$task_status', '$task_cir')");
	$task_id = dbLastID($dbh);
		
    // Flag for whether the leader was added to the assignments :
	$manager_added = false;
		
		/*
	//check if the leader was added to the assignments
	while (list(,$username) = each($assigned)) {
		if ($username == $project_leader)
		{
    		$leader_added = true;
		}
		dbQuery("INSERT INTO $ASSIGNMENTS_TABLE VALUES ($proj_id, '$username')");
		dbQuery("INSERT INTO $TASK_ASSIGNMENTS_TABLE(proj_id, task_id, username) VALUES ($proj_id, $task_id, '$username')");
	}
	if (!$leader_added) {
		// Add the project leader.
		dbQuery("INSERT INTO $ASSIGNMENTS_TABLE VALUES ($proj_id, '$project_leader')");
		dbQuery("INSERT INTO $TASK_ASSIGNMENTS_TABLE(proj_id, task_id, username) VALUES ($proj_id, $task_id, '$project_leader')");
	}
	*/
	
	/*
	if (isset($assigned)) {
		while (list(,$username) = each($assigned)) {
		    if ($username == $manager)
		    {
    	    	$manager_added = true;
		    }
			dbQuery("INSERT INTO $TASK_ASSIGNMENTS_TABLE (proj_id, task_id, username) VALUES ($proj_id, $task_id, '$username')");
		}	
	}	
	if (!$manager_added) {
		// Add the manager leader:
        dbQuery("INSERT INTO $TASK_ASSIGNMENTS_TABLE (proj_id, task_id, username) VALUES ($proj_id, $task_id, '$manager')");
	}*/
	if (isset($assigned)) {
		while (list(,$username) = each($assigned)) {
		    if ($username == $manager)
		    {
    	    	$manager_added = true;
		    }
		
			dbQuery("INSERT INTO $TASK_ASSIGNMENTS_TABLE(proj_id, task_id, username) VALUES ($proj_id, $task_id, '$username')");
			
			list($qhBis,$numBis) = dbQuery("select proj_id, username from $ASSIGNMENTS_TABLE where proj_id=$proj_id and username='$username'");
			if ($numBis < 1) {
			    dbQuery("INSERT INTO $ASSIGNMENTS_TABLE(proj_id, username)   VALUES ($proj_id, '$username')");
			}
			
			
		}
	}
	
	if (!$manager_added) {
	
	    list($qhBis,$numBis) = dbQuery("select proj_id, username from $ASSIGNMENTS_TABLE where proj_id=$proj_id and username='$manager'");
	    if ($numBis < 1) {
	        dbQuery("INSERT INTO $ASSIGNMENTS_TABLE   VALUES ($proj_id, '$manager')");
	    }
		// Add the manager leader:
        dbQuery("INSERT INTO $TASK_ASSIGNMENTS_TABLE (proj_id, task_id, username) VALUES ($proj_id, $task_id, '$manager')");
	}
	
	// Redirect to the task management page (we're done):
	Header("Location: task_maint.php?proj_id=$proj_id");
} 
elseif ($action == "edit") {
	$name        = addslashes($name);
	$description = addslashes($description);

	$query = "UPDATE $TASK_TABLE set name='$name', identifier='$identifier',description='$description', source_project='$source_project', target_project='$target_project', manager='$manager', ".
				" status='$task_status', cir='$task_cir'  ".
				" where task_id=$task_id";
    // Flag for whether the leader was added to the assignments :
	$manager_added = false;
	list($qh,$num) = dbquery($query);
    
	if ($assigned) {
		dbQuery("Delete from $TASK_ASSIGNMENTS_TABLE where task_id = $task_id");
		while (list(,$username) = each($assigned)) {
		    if ($username == $manager)
		    {
    	    	$manager_added = true;
		    }
		
			dbQuery("INSERT INTO $TASK_ASSIGNMENTS_TABLE(proj_id, task_id, username) VALUES ($proj_id, $task_id, '$username')");
			
			list($qhBis,$numBis) = dbQuery("select proj_id, username from $ASSIGNMENTS_TABLE where proj_id=$proj_id and username='$username'");
			if ($numBis < 1) {
			    dbQuery("INSERT INTO $ASSIGNMENTS_TABLE(proj_id, username)   VALUES ($proj_id, '$username')");
			}
			
			
		}
	}
	/*
	list($qhBis,$numBis) = dbQuery("select proj_id, username from $ASSIGNMENTS_TABLE where proj_id=$proj_id and username='$username'");
	if ($numBis < 1) {
	    dbQuery("INSERT INTO $ASSIGNMENTS_TABLE(proj_id, username)   VALUES ($proj_id, '$username')");
	}*/

	if (!$manager_added) {
	
	    list($qhBis,$numBis) = dbQuery("select proj_id, username from $ASSIGNMENTS_TABLE where proj_id=$proj_id and username='$manager'");
	    if ($numBis < 1) {
	        dbQuery("INSERT INTO $ASSIGNMENTS_TABLE   VALUES ($proj_id, '$manager')");
	    }
		// Add the manager leader:
        dbQuery("INSERT INTO $TASK_ASSIGNMENTS_TABLE (proj_id, task_id, username) VALUES ($proj_id, $task_id, '$manager')");
	}	
	
	
	// we're done so redirect to the task management page
	Header("Location: task_maint.php?proj_id=$proj_id");
} 
elseif ($action == 'delete') {
	dbQuery("delete from $TASK_TABLE where task_id = $task_id");
	dbQuery("delete from $TASK_ASSIGNMENTS_TABLE where task_id = $task_id");
	Header("Location: task_maint.php?proj_id=$proj_id");
} 
?>

    


